package com.whfp.coky.ui.entry

import android.view.View
import com.amap.api.location.AMapLocation
import com.hjq.permissions.Permission
import com.hjq.toast.ToastUtils
import com.tencent.mmkv.MMKV
import com.whfp.coky.aop.Permissions
import com.whfp.coky.databinding.ActivityActivationBinding
import com.whfp.coky.db.DbFlowTools
import com.whfp.coky.db.dao.ProjectSettingDao
import com.whfp.coky.event.EventCode
import com.whfp.coky.event.EventMessage
import com.whfp.coky.other.Constants
import com.whfp.coky.ui.base.BaseActivity
import com.whfp.coky.ui.entry.model.ActivationToke
import com.whfp.coky.ui.entry.vm.ActivationVM
import com.whfp.coky.ui.task.act.ScanCodeAct
import com.whfp.coky.ui.task.model.FluorSettingBean
import com.whfp.coky.utils.GsonUtils
import com.whfp.coky.utils.InfoTool
import com.whfp.coky.utils.LocationTool


class ActivationAct : BaseActivity<ActivationVM, ActivityActivationBinding>(){

    val mmkv = MMKV.defaultMMKV()
    var encodeSource:ActivationToke? =null//加密源

    override fun isStatusBarDarkFont(): Boolean {
        return !super.isStatusBarDarkFont()
    }

    override fun initView() {
        setOnClickListener(vBinding.llActivation)
//        vBinding.marquee.setContent("技术支持——臻稀生物科技有限公司                    ")

    }

    override fun initData() {
        initProjDatas()
    }

    //    初始化项目数据
    @Permissions( Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE)
    private fun initProjDatas() {
//    需要判断第一次进入。
        if (mmkv.decodeBool(Constants.FIRST,true)){
            loadFluorSettings()//加载fluor设置信息
            loadSQLBase()//加载数据基础数据
            storeUrls()//保存基础的url
            mmkv.encode(Constants.FIRST,false)
        }
    }

    override fun handleEvent(msg: EventMessage) {
        super.handleEvent(msg)
        when(msg.code){
            EventCode.SCAN_CODE -> {
                encodeSource?.linkSys = msg.msg
                InfoTool.generateDescToken(encodeSource!!)?.let {
                    vModel.doActivation(it)
                }
            }
            EventCode.LOCATION -> {
                hideDialog()
                toast("location success...act get result...")
                encodeSource = ActivationToke().apply {
                    this.lat = (msg.obj as AMapLocation).latitude.toString()
                    this.lng = (msg.obj as AMapLocation).longitude.toString()
                    this.loction = (msg.obj as AMapLocation).address.toString()
                }
                ScanCodeAct.start(this,Constants.INTENT_TYPE_ACTIVATION_SM,null)
            }
        }
    }

    @Permissions(Permission.ACCESS_COARSE_LOCATION,Permission.ACCESS_FINE_LOCATION)
    override fun onClick(view: View) {
        super.onClick(view)
        when(view){
            vBinding.llActivation ->   {
//              定位启动
                showDialog("定位中..")
                LocationTool(this).doLoc()


//                ScanCodeAct.start(this,Constants.INTENT_TYPE_ACTIVATION_SM,null)

//                vModel.doActivation()
//                toast("激活成功")
//                postDelayed({
//                    startActivity(LoginSelectAct::class.java)
//                    finish() },500)
            }
        }
    }


    private fun loadFluorSettings() {
        //所有设置信息
        mmkv.encode(Constants.MMKV_FLUOR_SETTINGS, FluorSettingBean())
    }
    private fun loadSQLBase() {
        DbFlowTools.insertProjectSettingList(GsonUtils.jsonToArrayList(
            GsonUtils.readAssetsJson(this, "default_project_setting.json"),
            ProjectSettingDao::class.java
        ))
    }
    private fun storeUrls() {
        //臻稀检测的url信息
        mmkv.encode(Constants.URL_LOGIN, Constants.URL_LOGIN)
        mmkv.encode(Constants.URL_UPLOAD_DATA, Constants.URL_UPLOAD_DATA)
        mmkv.encode(Constants.URL_UPLOAD_FILE, Constants.URL_UPLOAD_FILE)
        mmkv.encode(Constants.URL_USER_NAME, Constants.URL_USER_NAME)
        mmkv.encode(Constants.URL_USER_PWD, Constants.URL_USER_PWD)
    }
}